/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.server.common;

import cn.ac.iie.di.wx.server.configuration.Configuration;
import cn.ac.iie.di.wx.server.vo.OracleUpdateVo;
import cn.ac.iie.di.wx.server.vo.RedisVo;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    //定义运行时参数及其对应的配置环境的变量名
    public static final String IP = "ip";
    public static final String PORT = "port";
    public static final String THREADNUM = "threadNum";
    public static final String addUserFriendCircleMonitor = "addUserFriendCircleMonitor";
    public static final String delUserFriendCircleMonitor = "delUserFriendCircleMonitor";
    public static final String addUnicontrol = "addUnicontrol";
    public static final String delUnicontrol = "delUnicontrol";
    public static final String OCRQuery = "OCRQuery";
    public static final String FCMsgByKeywordQuery = "FCMsgByKeywordQuery";
    public static final String friendCircleMsgQuery = "friendCircleMsgQuery";
    public static final String groupMediaQuery = "groupMediaQuery";
    public static final String picByPQuery = "picByPQuery";

    public static final String redis_hosts = "redis_hosts";
    public static final String redis_masterName = "redis_masterName";
    public static final String redis_dbName = "redis_dbName";

    public static final String areaid = "areaid";
    public static final String userareaid = "userareaid";
    public static final String sysid = "sysid";
    public static final String sysuserid = "sysuserid";

    public static AtomicInteger addUFCMCount = new AtomicInteger(0);
    public static AtomicInteger delUFCMCount = new AtomicInteger(0);
    public static AtomicInteger addUNCTCount = new AtomicInteger(0);
    public static AtomicInteger delUNCTCount = new AtomicInteger(0);

    public static AtomicInteger OCRCount = new AtomicInteger(0);
    public static AtomicInteger FCMBKCount = new AtomicInteger(0);
    public static AtomicInteger FCMCount = new AtomicInteger(0);
    public static AtomicInteger PBPCount = new AtomicInteger(0);
    //添加oracle入库成功统计
    public static AtomicInteger ORC_OCRCount = new AtomicInteger(0);
    public static AtomicInteger ORC_PBPCount = new AtomicInteger(0);
    //存放下载信息的队列，用于发送给下载线程进行ftp下载
//    public static LinkedBlockingQueue<FtpDownloadFileVo> ftpDownloadFileVo = new LinkedBlockingQueue<FtpDownloadFileVo>(200);
    public static LinkedBlockingQueue<RedisVo> redisVos = new LinkedBlockingQueue<RedisVo>(200);
    public static LinkedBlockingQueue<RedisVo> retryQueue = new LinkedBlockingQueue<RedisVo>(64);
    public static LinkedBlockingQueue<OracleUpdateVo> oracleUpdateQueue = new LinkedBlockingQueue<OracleUpdateVo>(200);
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        //使用添加函数进行运行时参数设置
        setKeyAndValue(IP, "ip");
        setKeyAndValue(PORT, "port");
        setKeyAndValue(THREADNUM, "threadNum");

        setKeyAndValue(addUserFriendCircleMonitor, "addUserFriendCircleMonitor");
        setKeyAndValue(delUserFriendCircleMonitor, "delUserFriendCircleMonitor");
        setKeyAndValue(addUnicontrol, "addUnicontrol");
        setKeyAndValue(delUnicontrol, "delUnicontrol");
        setKeyAndValue(OCRQuery, "OCRQuery");
        setKeyAndValue(FCMsgByKeywordQuery, "FCMsgByKeywordQuery");
        setKeyAndValue(friendCircleMsgQuery, "friendCircleMsgQuery");
        setKeyAndValue(groupMediaQuery, "groupMediaQuery");
        setKeyAndValue(picByPQuery, "picByPQuery");

        setKeyAndValue(redis_hosts, "redis_hosts");
        setKeyAndValue(redis_masterName, "redis_masterName");
        setKeyAndValue(redis_dbName, "redis_dbName");

        setKeyAndValue(areaid, "areaid");
        setKeyAndValue(userareaid, "userareaid");
        setKeyAndValue(sysid, "sysid");
        setKeyAndValue(sysuserid, "sysuserid");

        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
